﻿#include "pch.h"


////unicodeתascii
//CXTextA WoCodingWToA(const wchar_t* wp, size_t m_encode)
//{
//	int len = WideCharToMultiByte(m_encode, 0, wp, wcslen(wp), NULL, 0, NULL, NULL);
//	char* l_char = (char*)malloc(len + 1);
//	WideCharToMultiByte(m_encode, 0, wp, wcslen(wp), l_char, len, NULL, NULL);
//	l_char[len] = '\0';
//	CXTextA rets = l_char;
//	free(l_char);
//	return  rets;
//}

//linux下用iconv

void _WoCodingGBK2UTF8_Win1(const char* gbkStr,CXTextA& retStr) {
	int n = MultiByteToWideChar(CP_ACP, 0, gbkStr, -1, NULL, 0);
	WCHAR* str1 = new WCHAR[n];
	MultiByteToWideChar(CP_ACP, 0, gbkStr, -1, str1, n);
	n = WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL);
	char* str2 = new char[n];
	WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, n, NULL, NULL);
	retStr = str2;
	delete[]str1;
	str1 = NULL;
	delete[]str2;
	str2 = NULL;
}

void _WoCodingUTF82GBK_Win1(const char* utf8Str, CXTextA& retStr) {
	int n = MultiByteToWideChar(CP_UTF8, 0, utf8Str, -1, NULL, 0);
	WCHAR* str1 = new WCHAR[n];
	MultiByteToWideChar(CP_UTF8, 0, utf8Str, -1, str1, n);
	n = WideCharToMultiByte(CP_ACP, 0, str1, -1, NULL, 0, NULL, NULL);
	char* str2 = new char[n];
	WideCharToMultiByte(CP_ACP, 0, str1, -1, str2, n, NULL, NULL);
	retStr = str2;
	delete[] str1;
	str1 = NULL;
	delete[] str2;
	str2 = NULL;
}

CXTextA _WoCodingGBK2UTF8_Win(const char* gbkStr) {
	std::string outStr;
	int n = MultiByteToWideChar(CP_ACP, 0, gbkStr, -1, NULL, 0);
	WCHAR* str1 = new WCHAR[n];
	MultiByteToWideChar(CP_ACP, 0, gbkStr, -1, str1, n);
	n = WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL);
	char* str2 = new char[n];
	WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, n, NULL, NULL);
	outStr = str2;
	delete[]str1;
	str1 = NULL;
	delete[]str2;
	str2 = NULL;
	return outStr;
}

CXTextA _WoCodingUTF82GBK_Win(const char* utf8Str) {
	std::string outStr;
	int n = MultiByteToWideChar(CP_UTF8, 0, utf8Str, -1, NULL, 0);
	WCHAR* str1 = new WCHAR[n];
	MultiByteToWideChar(CP_UTF8, 0, utf8Str, -1, str1, n);
	n = WideCharToMultiByte(CP_ACP, 0, str1, -1, NULL, 0, NULL, NULL);
	char* str2 = new char[n];
	WideCharToMultiByte(CP_ACP, 0, str1, -1, str2, n, NULL, NULL);
	outStr = str2;
	delete[] str1;
	str1 = NULL;
	delete[] str2;
	str2 = NULL;
	return outStr;
}